// pages/timeCar/index.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: null,
    mainpic: "",
    current: 0,
    num: 1,
    viewId: 'dots0',
    piclist: [
      
    ],
    product: {},
    storeId:0,
    storeName:'',
    player:0,
    navbg:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options)
    this.setData({
      id: options.data,
      storeId:options.storeId,
      storeName:options.storeName
    }, () => {
      this.proDetails()
    })
  },

  proDetails(){
    const { id } = this.data
    app.agriknow.proDetails({id}).then(res => {
      if(res.code == '000000'){
   
        var datas=res.data
        if(datas.bannerImage !== null && datas.bannerImage !== "" && datas.bannerImage != []){
          datas.bannerImage = datas.bannerImage.split(',');
        }
        if(datas.detailsImage !== null && datas.detailsImage !== "" && datas.detailsImage != []){
          datas.detailsImage = datas.detailsImage.split(',');
        }

        // if(res.data.specifications !== null && res.data.specifications !== "" && res.data.specifications != []){
        //   specifications = res.data.specifications.split(',');
        // }
        this.setData({
          product: {...datas},
          //piclist: image
        })
      }else{
        wx.showToast({
          title: res.message,
          icon:'none',
          duration:1500
        })
      }
    })
  },

  handlechange(e){
    this.setData({
      current: e.detail.current,
      viewId: 'dots' + e.detail.current
    })
  },

  handlestrpChange(e){
    this.setData({
      num: e.detail
    })
  },

  handleadd(){
    this.setData({
      num: 1
    })
  },

  handledotclick(e){
    this.setData({
      current: e.currentTarget.dataset.current
    })
  },

  submit(){
    this.setData({
      player:0
    })
    var userLogin= wx.getStorageSync('userLogin')
    if (!userLogin.mobile) {
      wx.navigateTo({
        url: '/pages/login/index',
      })
      return false
    }
    if(this.data.num==0) return
    let data = {
      num:this.data.num,
      packageId:parseInt(this.data.id),
      price:this.data.num*this.data.product.price,
      remake:this.data.product.price,
      storeId:this.data.product.storeId,
      userId:wx.getStorageSync('userLogin').userId,
      type:1
    }
    app.agriknow.userBuy(data).then(res=>{
      console.log(res)
      var id=res.data
      let object = {
        id:id,
        price:data.price,
        proList:[
          {
            id:data.packageId,
            num:data.num,
            price:this.data.product.price,
            productImage:this.data.product.image,
            productName:this.data.product.name
          }
        ],
        remake:data.remake,
        storeName:this.data.product.storeName,
      }
      wx.navigateTo({
        url: '/pages/submitDetail/index?id='+id+'&type=1',
        success:function(res){
          //res.eventChannel.emit('submitDetail', {id:object.id,object:object,type:1})
        }
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
 
  },

  back(){
    wx.navigateBack({
      delta: 1
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return{
      title:this.data.product.name,
      imageUrl:this.data.product.image
    }
  },
  reduce:function(e){
    var num=this.data.num
    if(num>1){
      this.setData({
        num:num-1
      })
    }
  },
  add:function(e){
    var num=this.data.num
   
      this.setData({
        num:num+1
      })
    
  },
  toBuy:function(){
    this.setData({
      player:1
    })
  },
  close:function(){
    this.setData({
      player:0
    })
  },
  onPageScroll: function (e) {

    if (e.scrollTop > 20) {
      this.setData({ 
        navbg:true
      })
    } else {
      this.setData({ 
        navbg:false
      })
    }
  }, 
  toStore:function(e){
    var product=this.data.product
    wx.navigateTo({
      url: '/pages/storeDetail/index?data='+product.storeId,
    })
  }
})